
package com.epfresh.supplier.adapter;

import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.TextView;

import com.epfresh.api.universalimageloader.core.ImageLoader;
import com.epfresh.api.universalimageloader.utils.Log;
import com.epfresh.supplier.R;
import com.epfresh.supplier.entity.*;

import multitype.ItemViewBinder;
import multitype.OnRecyclerViewItemClickListener;

public class AgentMyRecordViewBinder
    extends ItemViewBinder<AgentMyRecord.ProductsBean, AgentMyRecordViewBinder.ViewHolder> implements View.OnClickListener{

    OnRecyclerViewItemClickListener onRecyclerViewItemClickListener;

    public OnRecyclerViewItemClickListener getOnRecyclerViewItemClickListener() {
        return onRecyclerViewItemClickListener;
    }

    public void setOnRecyclerViewItemClickListener(OnRecyclerViewItemClickListener onRecyclerViewItemClickListener) {
        this.onRecyclerViewItemClickListener = onRecyclerViewItemClickListener;
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(
            @NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View view = inflater.inflate(R.layout.item_agent_my_record, parent, false);
        return new ViewHolder(view);
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull AgentMyRecord.ProductsBean post) {
        if(post.isMoreFlag()) {
            holder.rl_agent_record_more.setVisibility(View.VISIBLE);
        } else {
            holder.rl_agent_record_more.setVisibility(View.GONE);
        }
        if(post.isFirstFlag()) {
            holder.rl_agent_record_base.setVisibility(View.VISIBLE);
            holder.tv_agent_record_base.setText(post.getFather().getName());

            //店铺代办
            switch (post.getFather().getAgentStatus()) {
                case 1:
                    holder.sw_agent_record.setChecked(true);
                    holder.tv_agent_record_state.setText("申请中");
                    break;
                case 2:
                    holder.sw_agent_record.setChecked(true);
                    holder.tv_agent_record_state.setText("代办中");
                    holder.tv_agent_record_state.setTextColor(Color.RED);
                    break;
                case 3:
                    holder.sw_agent_record.setChecked(false);
                    holder.tv_agent_record_state.setText("拒绝代办");
                    break;
                case 4:
                    holder.sw_agent_record.setChecked(false);
                    holder.tv_agent_record_state.setText("暂停代办");
                    holder.tv_agent_record_state.setTextColor(Color.parseColor("#666666"));
                    break;
//                default:
//                    holder.sw_agent_record.setVisibility(View.GONE);
//                    holder.tv_agent_record_state.setVisibility(View.GONE);
//                    break;
            }
        }
        else {
            holder.rl_agent_record_base.setVisibility(View.GONE);
        }

        if (post.getFather() != null) {
            if (!post.getFather().isEnabled() || post.getFather().getAgentStatus() == 5) {
                holder.rl_agent_freeze.setVisibility(View.VISIBLE);
                holder.rl_agent_record_base.setVisibility(View.GONE);
                if (post.getFather().getAgentStatus() == 5) {
                    holder.txt_agent_record_freeze.setText(post.getFather().getName());
                }else {
                    holder.txt_agent_record_freeze.setText(post.getFather().getName() + "(冻结)");
                }
            } else {
                holder.rl_agent_freeze.setVisibility(View.GONE);


            }
            //店铺代办
            switch (post.getFather().getAgentStatus()) {
                case 1:
                    holder.txt_agent_state_base.setText("申请中");
                    break;
                case 2:
                    holder.txt_agent_state_base.setText("代办中");
                    break;
                case 3:
                    holder.txt_agent_state_base.setText("拒绝代办");
                    break;
                case 4:
                    holder.txt_agent_state_base.setText("暂停代办");
                    break;
                case 5:
                    holder.txt_agent_state_base.setText("解除中");
                    break;
            }

        }

        ImageLoader.getInstance().displayImage(post.getThumbnail(),holder.iv_agent_record);
        holder.tv_agent_record_title.setText(post.getTitle());
        holder.tv_agent_record_price.setText(post.getMinPrice()+"元~"+post.getMaxPrice()+"元/"+post.getChargeUnit());
        holder.tv_agent_record_money.setText("代办费："+post.getCommission()+"%");
        //holder.tv_agent_record_size.setText(post.getMinOrderQuantity()+post.getChargeUnit()+"起发货/上限"+post.getMaxOrderQuantity()+post.getChargeUnit());
        holder.tv_agent_record_size.setText("满"+post.getMinOrderQuantity()+post.getChargeUnit()+"可发货，最高可拼"+post.getMaxOrderQuantity()+post.getChargeUnit());
        holder.tv_agent_record_package.setText(post.getPack());
        holder.tv_agent_record_title.setText(post.getTitle());
        //商品代办
        if (post.getState()!=null){
            switch (post.getState()) {
                case 0:
                    holder.goodsAgentRecordSw.setChecked(false);
                    holder.goodsAgentRecordTv.setText("暂停代办");
                    holder.goodsAgentRecordTv.setTextColor(Color.parseColor("#666666"));
                    holder.tv_agent_record_money.setTextColor(Color.parseColor("#666666"));
                    break;
                case 1:
                    holder.goodsAgentRecordSw.setChecked(true);
                    holder.goodsAgentRecordTv.setText("代办中");
                    holder.goodsAgentRecordTv.setTextColor(Color.RED);
                    holder.tv_agent_record_money.setTextColor(Color.RED);
                    break;
            }
        }

        if (post.getTitle() == null) {
            holder.rl_agent_record_goods.setVisibility(View.GONE);
        } else {
            holder.rl_agent_record_goods.setVisibility(View.VISIBLE);
        }

        holder.tv_agent_record_go.setTag(R.id.item_key_position,-1);
        holder.tv_agent_record_go.setTag(R.id.item_key_status,post);
        holder.tv_agent_record_go.setOnClickListener(this);
        holder.sw_agent_record.setTag(R.id.item_key_position,getPosition(holder));
        holder.sw_agent_record.setTag(R.id.item_key_status,post);
        holder.sw_agent_record.setOnClickListener(this);
        holder.tv_agent_modify.setTag(R.id.item_key_position,-1);
        holder.tv_agent_modify.setTag(R.id.item_key_status,post);
        holder.tv_agent_modify.setOnClickListener(this);
        //单个商品代办
        holder.goodsAgentRecordSw.setTag(R.id.item_key_position,-1);
        holder.goodsAgentRecordSw.setTag(R.id.item_key_status,post);
        holder.goodsAgentRecordSw.setOnClickListener(this);

        holder.rl_agent_record_goods.setTag(R.id.item_key_position,-1);
        holder.rl_agent_record_goods.setTag(R.id.item_key_status,post);
        holder.rl_agent_record_goods.setOnClickListener(this);

        holder.rl_agent_record_more.setTag(R.id.item_key_position,-1);
        holder.rl_agent_record_more.setTag(R.id.item_key_status,post);
        holder.rl_agent_record_more.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        Object tag = v.getTag(R.id.item_key_position);
        if (tag != null && tag instanceof Integer){
            int position = (int) tag;
            Object o = v.getTag(R.id.item_key_status);
            if (onRecyclerViewItemClickListener != null && o instanceof AgentMyRecord.ProductsBean){
                onRecyclerViewItemClickListener.onItemClick(v,position,o);
            }
        }
    }


    static class ViewHolder extends RecyclerView.ViewHolder {


        private View rl_agent_record_base,rl_agent_record_goods,rl_agent_record_more,rl_agent_freeze;
        private Switch sw_agent_record;
        private ImageView iv_agent_record;
        private TextView tv_agent_modify,tv_agent_record_base,tv_agent_record_go,tv_agent_record_state,tv_agent_record_title,tv_agent_record_price,tv_agent_record_money,tv_agent_record_size,tv_agent_record_package,txt_agent_record_freeze,txt_agent_state_base;
        private Switch goodsAgentRecordSw;
        private TextView goodsAgentRecordTv;


        ViewHolder(@NonNull View itemView) {
            super(itemView);
            rl_agent_record_more = itemView.findViewById(R.id.rl_agent_record_more);
            rl_agent_record_goods = itemView.findViewById(R.id.rl_agent_record_goods);
            rl_agent_record_base = itemView.findViewById(R.id.rl_agent_record_base);
            rl_agent_freeze = itemView.findViewById(R.id.rl_agent_freeze);
            sw_agent_record = itemView.findViewById(R.id.sw_agent_record);
            iv_agent_record = itemView.findViewById(R.id.iv_agent_record);

            tv_agent_modify = itemView.findViewById(R.id.tv_agent_modify);

            tv_agent_record_base = itemView.findViewById(R.id.tv_agent_record_base);
            tv_agent_record_go = itemView.findViewById(R.id.tv_agent_record_go);
            tv_agent_record_state = itemView.findViewById(R.id.tv_agent_record_state);

            tv_agent_record_title = itemView.findViewById(R.id.tv_agent_record_title);
            tv_agent_record_price = itemView.findViewById(R.id.tv_agent_record_price);
            tv_agent_record_money = itemView.findViewById(R.id.tv_agent_record_money);
            tv_agent_record_size = itemView.findViewById(R.id.tv_agent_record_size);
            tv_agent_record_package = itemView.findViewById(R.id.tv_agent_record_package);
            txt_agent_record_freeze = itemView.findViewById(R.id.txt_agent_record_freeze);
            txt_agent_state_base = itemView.findViewById(R.id.txt_agent_state_base);
            //添加了单个商品代办
            goodsAgentRecordSw=itemView.findViewById(R.id.goods_agent_record_sw);
            goodsAgentRecordTv=itemView.findViewById(R.id.goods_agent_record_state_tv);
        }
    }
}
